home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dmake / Localized-Source / depend.c < prev    next >
C/C++ Source or Header  |  1993-01-10  |  6KB  |  250 lines

  1.  
  2. /*
  3.  *  DEPEND.C
  4.  */
  5.  
  6. #include "defs.h"
  7.  
  8. Prototype void InitDep();
  9. Prototype DepRef  *CreateDepRef(List *, char *);
  10. Prototype DepCmdList *AllocDepCmdList(void);
  11. Prototype DepRef  *DupDepRef(DepRef *);
  12. Prototype void      IncorporateDependency(DepRef *, DepRef *, List *);
  13. Prototype int      ExecuteDependency(DepRef *, time_t *);
  14.  
  15. Prototype List DepList;
  16.  
  17. List DepList;
  18.  
  19. void
  20. InitDep()
  21. {
  22.     NewList(&DepList);        /*    master list */
  23. }
  24.  
  25. DepRef *
  26. CreateDepRef(list, name)
  27. List *list;
  28. char *name;
  29. {
  30.     DepRef *ref;
  31.     DepNode *dep;
  32.  
  33.     for (dep = GetTail(&DepList); dep; dep = GetPred(&dep->dn_Node)) {
  34.     if (strcmp(name, dep->dn_Node.ln_Name) == 0)
  35.         break;
  36.     }
  37.     if (dep == NULL) {
  38.     dep = malloc(sizeof(DepNode) + strlen(name) + 1);
  39.     clrmem(dep, sizeof(DepNode));
  40.     dep->dn_Node.ln_Name = (char *)(dep + 1);
  41.     NewList(&dep->dn_DepCmdList);
  42.     strcpy(dep->dn_Node.ln_Name, name);
  43.     AddTail(&DepList, &dep->dn_Node);
  44.     }
  45.  
  46.     ref = malloc(sizeof(DepRef));
  47.     clrmem(ref, sizeof(DepRef));
  48.  
  49.     ref->rn_Node.ln_Name = dep->dn_Node.ln_Name;
  50.     ref->rn_Dep = dep;
  51.     AddTail(list, &ref->rn_Node);
  52.     return(ref);
  53. }
  54.  
  55.  
  56. DepRef *
  57. DupDepRef(ref0)
  58. DepRef *ref0;
  59. {
  60.     DepRef *ref = malloc(sizeof(DepRef));
  61.  
  62.     clrmem(ref, sizeof(DepRef));
  63.     ref->rn_Node.ln_Name = ref0->rn_Node.ln_Name;
  64.     ref->rn_Dep = ref0->rn_Dep;
  65.     return(ref);
  66. }
  67.  
  68. void
  69. IncorporateDependency(lhs, rhs, cmdList)
  70. DepRef *lhs;
  71. DepRef *rhs;
  72. List *cmdList;
  73. {
  74.     DepNode *dep = lhs->rn_Dep;     /*    source master */
  75.     DepCmdList *depCmdList = GetHead(&dep->dn_DepCmdList);
  76.  
  77.     if (depCmdList == NULL || depCmdList->dc_CmdList != cmdList) {
  78.     depCmdList = malloc(sizeof(DepCmdList));
  79.     clrmem(depCmdList, sizeof(DepCmdList));
  80.     NewList(&depCmdList->dc_RhsList);
  81.     depCmdList->dc_CmdList = cmdList;
  82.     AddHead(&dep->dn_DepCmdList, &depCmdList->dc_Node);
  83.     }
  84.  
  85.     if (rhs)
  86.     AddTail(&depCmdList->dc_RhsList, &rhs->rn_Node);
  87.  
  88.     db3printf(("Incorporate: %s -> %s\n", dep->dn_Node.ln_Name, (rhs) ? rhs->rn_Node.ln_Name : ""));
  89.  
  90. }
  91.  
  92. /*
  93.  *  Execute dependency returning an error and time completion code (time
  94.  *  completion code is 0 if object does not exit or had to be 'run')
  95.  */
  96.  
  97. int
  98. ExecuteDependency(ref, pt)
  99. DepRef *ref;
  100. time_t *pt;
  101. {
  102.     DepNode *dep = ref->rn_Dep;
  103.     int r = 0;
  104.  
  105.     dbprintf(("Go %s:\n", dep->dn_Node.ln_Name));
  106.  
  107.     if (dep->dn_Node.ln_Type != NT_RESOLVED) {
  108.     DepCmdList *depCmdList;
  109.     short statok = 0;
  110.     short masterForce = 0;
  111.     struct stat sbuf;
  112.  
  113.     dep->dn_Node.ln_Type = NT_RESOLVED;
  114.  
  115.     /*
  116.      *  sub-dependency groups are handled individually
  117.      */
  118.  
  119.     if (stat(dep->dn_Node.ln_Name, &sbuf) == 0) {
  120.         statok = 1;
  121.         dep->dn_Time = sbuf.st_mtime;
  122.     }
  123.  
  124.     for (depCmdList = GetHead(&dep->dn_DepCmdList); r == 0 && depCmdList; depCmdList = GetSucc(&depCmdList->dc_Node)) {
  125.         short force;
  126.  
  127.         /*
  128.          *    A lower level dependency that gets hit but has no command
  129.          *    list will force the next higher level dependency to get
  130.          *    hit.
  131.          */
  132.  
  133.         if (masterForce == 2) {
  134.         masterForce = 1;
  135.         force = 1;
  136.         } else {
  137.         force = DoAll;
  138.         }
  139.  
  140.         *pt = 0;
  141.  
  142.         for (ref = GetHead(&depCmdList->dc_RhsList); r == 0 && ref; ref = GetSucc(&ref->rn_Node)) {
  143.         time_t t;
  144.  
  145.         if ((r = ExecuteDependency(ref, &t)) < 0)
  146.             break;
  147.         if (t == 0)
  148.             force = 1;
  149.         if (*pt == 0 || (long)(t - *pt) > 0)
  150.             *pt = t;        /*    latest    */
  151.         dbprintf(("LHS %s stok=%d rhs=%s time=%08lx\n",
  152.             dep->dn_Node.ln_Name,
  153.             statok,
  154.             ref->rn_Node.ln_Name,
  155.             t
  156.             ));
  157.         }
  158.         if (r == 0) {
  159.         if (statok) {
  160.             /*
  161.              *    if the file exists check the time agains the
  162.              *    collected sub-dependency/  If the sub-dep is
  163.              *    newer we force
  164.              *
  165.              *    if *pt is NULL we check to see if there were any
  166.              *    sub-dependancies or commands.  If so we force, other
  167.              *    wise we load *pt with the file time
  168.              */
  169.  
  170.             if (*pt) {
  171.             if ((long)(*pt - sbuf.st_mtime) > 0)
  172.                 force = 1;
  173.             } else if (GetHead(&depCmdList->dc_RhsList) || GetHead(depCmdList->dc_CmdList)) {
  174.             force = 1;
  175.             } else {
  176.             *pt = sbuf.st_mtime;
  177.  
  178.             }
  179.         } else {
  180.             /*
  181.              *
  182.              */
  183.                     /* Who knows why this code is here.  Matt seems to remember */
  184.                     /* a bug that had existed at one time.  This probably used  */
  185.                     /* to say something different.                              */
  186.             /* if (GetHead(&depCmdList->dc_RhsList)) { ********DEAD******/
  187.             force = 1;
  188.             /* } else {                                ********DEAD******/
  189.             /* force = 1;                              ********DEAD******/
  190.             /* }                                       ********DEAD******/
  191.         }
  192.  
  193.         dbprintf(("LHS %s stok=%d time=%08lx force= %d\n",
  194.             dep->dn_Node.ln_Name,
  195.             statok,
  196.             *pt,
  197.             force
  198.             ));
  199.  
  200.         /*
  201.          *  run command list if necessary.  If run then force result
  202.          *  to caller by setting *pt to 0
  203.          *
  204.          *  [re]create %(left) and %(right) variables
  205.          */
  206.  
  207.         if (force) {
  208.             dbprintf(("FORCE %s\n", dep->dn_Node.ln_Name));
  209.  
  210.             masterForce = 1;
  211.             if (GetHead(depCmdList->dc_CmdList)) {
  212.             Var *var;
  213.  
  214.             if (var = MakeVar("left", '%')) {
  215.                 PutCmdListSym(&var->var_CmdList, dep->dn_Node.ln_Name, NULL);
  216.             }
  217.             if (var = MakeVar("right", '%')) {
  218.                 short space = 0;
  219.  
  220.                 for (ref = GetHead(&depCmdList->dc_RhsList); r == 0 && ref; ref = GetSucc(&ref->rn_Node))
  221.                 PutCmdListSym(&var->var_CmdList, ref->rn_Node.ln_Name, &space);
  222.             }
  223.             SomeWork = 1;
  224.             if (ExecuteCmdList(dep, depCmdList->dc_CmdList) > 5) {
  225.                 r = -1;
  226.             }
  227.             } else {
  228.             masterForce = 2;
  229.             }
  230.         }
  231.         if (dep->dn_Time == 0 || (long)(*pt -  dep->dn_Time) > 0)
  232.             dep->dn_Time = *pt;
  233.         }
  234.     }
  235.     if (GetHead(&dep->dn_DepCmdList) == NULL) {
  236.         if (statok)
  237.         *pt = sbuf.st_mtime;
  238.     }
  239.     if (masterForce)
  240.         *pt = 0;
  241.     dep->dn_Time = *pt;
  242.     dbprintf(("FINAL %s statok=%d time=%08lx\n", dep->dn_Node.ln_Name, statok, *pt));
  243.     } else {
  244.     *pt = dep->dn_Time;
  245.     dbprintf(("DONE-ALREADY %s time=%08lx\n", dep->dn_Node.ln_Name, *pt));
  246.     }
  247.     return(r);
  248. }
  249.  
  250.